package com.xuesaieducation.zshddemo;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by yutf on 2018/1/15 0015.
 */

public class ScreenUtils
{
    public static boolean isPortrait(Context context)
    {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    }

    // 是否横屏
    public static boolean isLandscape(Context context)
    {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
    }

    // 设置竖屏
    public static void setPortrait(Activity activity)
    {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        Window window = activity.getWindow();
        if(window != null)
        {
//            window.getDecorView().
//                    setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);

            WindowManager.LayoutParams lp = window.getAttributes();
            lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            window.setAttributes(lp);
            window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }

    // 设置横屏
    public static void setLandscape(Activity activity)
    {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        Window window = activity.getWindow();
        if(window != null)
        {
//            window.getDecorView().
//                    setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);

            WindowManager.LayoutParams lp = window.getAttributes();
            lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            window.setAttributes(lp);
            window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }


    }

    public static int screenWidth(Context ctx)
    {
        return ctx.getResources().getDisplayMetrics().widthPixels;
    }

    public static int screenHeight(Context ctx)
    {
        return ctx.getResources().getDisplayMetrics().heightPixels;
    }
}
